package com.plugins.usereflect;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zxy.business.Ope_DB_Cent;
import com.zxy.common.Com_Fun;
import com.zxy.common.Com_Para;
import com.zxy.common.Db_Common_InterFace;
import com.zxy.common.PooledConnection_InterFace;
import com.zxy.model.T_PROC_NAME;

//遍历所有源码并查找接口是否已使用接口
public class A01_POROCU_IN implements com.zxy.common.ParamName_InterFace
{
	public StringBuilder	strResult		= new StringBuilder();
	public String			user_main_id			= null;
	public String			session_id		= null;
	public String			param_name		= "";
	//文件夹路径
	public String			param_value1	= null;
	//特殊文件
	public String			param_value2	= null;
	//过滤文件夹路径
	public String			param_value3	= null;
	//文件路径前缀隐藏1
	public String			param_value4	= null;
	//文件路径前缀隐藏2
	public String			param_value5	= null;
	public String			param_value6	= null;
	public String			param_value7	= null;
	public String			param_value8	= null;
	public String			param_value9	= null;
	public String			param_value10	= null;
	
	//包含文件夹
	public List<String> includeDir = new ArrayList<String>();
	//包含文件
	public List<String> includeFile = new ArrayList<String>();
	//过滤文件夹
	public List<String> noincludeDir = new ArrayList<String>();
	
	//文件路径前缀隐藏
	public String 			strRemoveRoot1 = "E:\\zxyong\\eclpise_webapp";
	public String 			strRemoveRoot2 = "E:\\xxxx\\webapp\\";
	
	public void init_start()
	{	
//		includeDir.add("E:\\zxyong\\eclpise_webapp\\CDIsys_v3\\biz_src");
//		includeDir.add("E:\\zxyong\\eclpise_webapp\\CDIsys_v3\\cache_src");
//		includeDir.add("E:\\zxyong\\eclpise_webapp\\CDIsys_v3\\jar_src");
//		includeDir.add("E:\\zxyong\\eclpise_webapp\\CDIsys_v3\\src");
//		includeDir.add("E:\\zxyong\\eclpise_webapp\\CDIsys_v3\\plugins_src");
//		
//		includeDir.add("E:\\zxyong\\eclpise_webapp\\CDIsys_v3\\webapp");
//		
//		noincludeDir.add("E:\\zxyong\\eclpise_webapp\\CDIsys_v3\\webapp\\css");
//		noincludeDir.add("E:\\zxyong\\eclpise_webapp\\CDIsys_v3\\webapp\\fonts");
//		noincludeDir.add("E:\\zxyong\\eclpise_webapp\\CDIsys_v3\\webapp\\img");
//		noincludeDir.add("E:\\zxyong\\eclpise_webapp\\CDIsys_v3\\webapp\\META-INF");
//		noincludeDir.add("E:\\zxyong\\eclpise_webapp\\CDIsys_v3\\webapp\\WEB-INF");
//		
//		includeFile.add("E:\\zxyong\\eclpise_webapp\\CDIsys_v3\\webapp\\js\\run_report.js");
//		includeFile.add("E:\\zxyong\\eclpise_webapp\\CDIsys_v3\\webapp\\js\\common.js");
		
		for(String strTem : this.param_value1.split("\\n")){
			includeDir.add(strTem);
		}
		for(String strTem : this.param_value2.split("\\n")){
			includeFile.add(strTem);
		}
		for(String strTem : this.param_value3.split("\\n")){
			noincludeDir.add(strTem);
		}

		strRemoveRoot1 = this.param_value4;
		strRemoveRoot2 = this.param_value5;
		
		// 线程异步调用
		Runnable Thread_A01_POROCU_IN = new Runnable(){
			@Override
			public void run(){
				System.out.println("==>遍历所有源码接口开始:"+Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss"));
				truncate_noused();
				
				//文件夹
				for(String fileDir : includeDir){
					FindDir(fileDir);
				}
				
				//文件
				for(String fileName : includeFile){
					ReadFile(fileName);
				}
	            Ins_t_proc_noused();
				System.out.println("==>遍历所有源码接口完成:"+Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss"));
			}
			
			//递归查找文件或文件夹
			public void FindDir(String fileDir){
				
				//是否过滤文件夹
				for(String noDir : noincludeDir){
					if(java.util.Objects.equals(noDir,fileDir))
						return;
				}
	            for(File file : new File(fileDir).listFiles()){
	                if(file.isDirectory()){
	                	FindDir(file.getAbsolutePath());
	                }else{
	                	ReadFile(file.getAbsolutePath());
	                }
	            }
			}
			
			//读取文件
			public void ReadFile(String FileName){
				File fiFile = new File(FileName);
				FileInputStream fr = null;
				if(!fiFile.exists()){
					return;
				}else
				{
					InputStreamReader is = null;
					BufferedReader br = null;
					try
					{
						fr = new FileInputStream(FileName);
						is = new InputStreamReader(fr,Com_Para.U_CODE);
						br = new BufferedReader(is);
						int iIndex = 1; 
						while(true)
						{
							String line = br.readLine();							
							for(T_PROC_NAME tpn : Com_Para.lst_T_PROC_NAME){
								if(!java.util.Objects.equals(line,"") 
									&& !java.util.Objects.equals(line,null)
									&& line.indexOf("\""+tpn.INF_EN_NAME+"\"") != -1){
									Ins_t_proc_used(tpn.INF_CN_NAME,tpn.INF_EN_NAME,FileName,iIndex,line);
								}
							}
							iIndex ++;
							if(java.util.Objects.equals(line,null))
								break;
						}
					}
					catch(Exception ex)
					{
						ex.printStackTrace();
					}
					finally
					{
						try
						{
							is.close();
							br.close();
							fr.close();
						}
						catch(Exception ex)
						{
						}
					}
				}
			}

			//清除数据
			public void truncate_noused(){
				// 调用存储过程 案例
				Db_Common_InterFace dbc = null;
				PooledConnection_InterFace pi = null;
				java.sql.ResultSet rs = null;
				StringBuilder sqlException = null;
				try
				{
					Ope_DB_Cent odc = new Ope_DB_Cent();
					Object dbcobject = odc.Get_Db_Common(1);
					dbc = (Db_Common_InterFace) (odc.Obj_To_Db_Common(dbcobject));
					StringBuilder strSql = new StringBuilder("truncate table t_proc_noused");
					dbc.CommonExec_Sql(strSql);
					
					strSql.delete(0,strSql.length());
					strSql.append("truncate table t_proc_used");
					dbc.CommonExec_Sql(strSql);
					
					// 执行获取sql异常信息
					sqlException = dbc.getSqlException();
					//这句一定要加
					pi = (PooledConnection_InterFace) dbc.getConn_a();
				}
				catch(Exception e)
				{
					Com_Fun.debug_in(e,this.toString());
				}
				finally
				{
					try
					{
						if(!java.util.Objects.equals(rs,null))
						{
							rs.getStatement().close();
							rs.close();
						}
						if(!java.util.Objects.equals(dbc,null) && !pi.isB_nocursor())
						{
							if(!java.util.Objects.equals(pi,null)
									&& !java.util.Objects.equals(pi.getCalls(),null))
								pi.getCalls().close();
						}else if(!java.util.Objects.equals(dbc,null))
						{
							pi.returnConnection();
						}
					}
					catch(SQLException e)
					{
						Com_Fun.debug_in(e,this.toString(),"db close");
					}
				}
			}
			
			//源码中未使用接口入库
			public void Ins_t_proc_noused(){
				// 调用存储过程 案例
				Db_Common_InterFace dbc = null;
				PooledConnection_InterFace pi = null;
				java.sql.ResultSet rs = null;
				StringBuilder sqlException = null;
				try
				{
					Ope_DB_Cent odc = new Ope_DB_Cent();
					Object dbcobject = odc.Get_Db_Common(1);
					dbc = (Db_Common_InterFace) (odc.Obj_To_Db_Common(dbcobject));
					String[] Parameters =
					{ };
					String[] ParamTypes =
					{ };
					String[] ParamOutName = new String[0];
					String[] ParamOutType = new String[0];
					T_PROC_NAME tpn = new T_PROC_NAME();
					tpn.INF_TYPE = "2";
					tpn.INF_EN_SQL = "insert into t_proc_noused(INF_CN_NAME,INF_EN_NAME,S_DESC,CREATE_DATE) "
							+" select INF_CN_NAME,INF_EN_NAME,'',now() from t_proc_name where inf_en_name not in (select inf_en_name from t_proc_used)";
					rs = dbc.Common_Sql_Proc("ins_t_proc_noused",Parameters,ParamTypes,ParamOutName,ParamOutType,tpn);
					
					// 执行获取sql异常信息
					sqlException = dbc.getSqlException();
					//这句一定要加
					pi = (PooledConnection_InterFace) dbc.getConn_a();
				}
				catch(Exception e)
				{
					Com_Fun.debug_in(e,this.toString());
				}
				finally
				{
					try
					{
						if(!java.util.Objects.equals(rs,null))
						{
							rs.getStatement().close();
							rs.close();
						}
						if(!java.util.Objects.equals(dbc,null) && !pi.isB_nocursor())
						{
							if(!java.util.Objects.equals(pi,null)
									&& !java.util.Objects.equals(pi.getCalls(),null))
								pi.getCalls().close();
						}else if(!java.util.Objects.equals(dbc,null))
						{
							pi.returnConnection();
						}
					}
					catch(SQLException e)
					{
						Com_Fun.debug_in(e,this.toString(),"db close");
					}
				}
			}

			//源码中已应用接口入库
			public void Ins_t_proc_used(String INF_CN_NAME,String INF_EN_NAME,String FILE_DIR,
					int LINES_NUM,String LINES_CONTENT){
				// 调用存储过程 案例
				Db_Common_InterFace dbc = null;
				PooledConnection_InterFace pi = null;
				java.sql.ResultSet rs = null;
				StringBuilder sqlException = null;
				try
				{
					Ope_DB_Cent odc = new Ope_DB_Cent();
					Object dbcobject = odc.Get_Db_Common(1);
					dbc = (Db_Common_InterFace) (odc.Obj_To_Db_Common(dbcobject));
					String[] Parameters =
					{ INF_CN_NAME,INF_EN_NAME,FILE_DIR.replace(strRemoveRoot1,"").replace(strRemoveRoot2,""),String.valueOf(LINES_NUM),LINES_CONTENT,"",Com_Fun.DateTimeStr("yyyy-MM-dd HH:mm:ss")};
					String[] ParamTypes =
					{ "STRING", "STRING", "STRING","INT","STRING","STRING","STRING"};
					String[] ParamOutName = new String[0];
					String[] ParamOutType = new String[0];
					T_PROC_NAME tpn = new T_PROC_NAME();
					tpn.INF_TYPE = "2";
					tpn.INF_EN_SQL = "insert into t_proc_used(INF_CN_NAME,INF_EN_NAME,FILE_DIR,LINES_NUM,LINES_CONTENT,S_DESC,CREATE_DATE) "
							+" values(?,?,?,?,?,?,?)";
					rs = dbc.Common_Sql_Proc("ins_t_proc_used",Parameters,ParamTypes,ParamOutName,ParamOutType,tpn);
					
					// 执行获取sql异常信息
					sqlException = dbc.getSqlException();
					//这句一定要加
					pi = (PooledConnection_InterFace) dbc.getConn_a();
				}
				catch(Exception e)
				{
					Com_Fun.debug_in(e,this.toString());
				}
				finally
				{
					try
					{
						if(!java.util.Objects.equals(rs,null))
						{
							rs.getStatement().close();
							rs.close();
						}
						if(!java.util.Objects.equals(dbc,null) && !pi.isB_nocursor())
						{
							if(!java.util.Objects.equals(pi,null)
									&& !java.util.Objects.equals(pi.getCalls(),null))
								pi.getCalls().close();
						}else if(!java.util.Objects.equals(dbc,null))
						{
							pi.returnConnection();
						}
					}
					catch(SQLException e)
					{
						Com_Fun.debug_in(e,this.toString(),"db close");
					}
				}
			}
		};

		// 异步调用
		Thread authThread = new Thread(Thread_A01_POROCU_IN);
		authThread.start();
		authThread.setName("线程Thread_A01_POROCU_IN");
		this.strResult.delete(0,this.strResult.length());
		this.strResult.append(Com_Fun.get_ope_result(param_name,"1","后台遍历所有源码并查找接口是否已使用,请等待"));
	}
	/**
	 * @return strResult
	 */
	public StringBuilder getStrResult()
	{
		return strResult;
	}
	/**
	 * @param strResult 要设置的 strResult
	 */
	public void setStrResult(StringBuilder strResult)
	{
		this.strResult = strResult;
	}
	/**
	 * @return user_main_id
	 */
	public String getUser_main_id()
	{
		return user_main_id;
	}
	/**
	 * @param user_main_id 要设置的 user_main_id
	 */
	public void setUser_main_id(String user_main_id)
	{
		this.user_main_id = user_main_id;
	}
	/**
	 * @return session_id
	 */
	public String getSession_id()
	{
		return session_id;
	}
	/**
	 * @param session_id 要设置的 session_id
	 */
	public void setSession_id(String session_id)
	{
		this.session_id = session_id;
	}
	/**
	 * @return param_name
	 */
	public String getParam_name()
	{
		return param_name;
	}
	/**
	 * @param param_name 要设置的 param_name
	 */
	public void setParam_name(String param_name)
	{
		this.param_name = param_name;
	}
	/**
	 * @return param_value1
	 */
	public String getParam_value1()
	{
		return param_value1;
	}
	/**
	 * @param param_value1 要设置的 param_value1
	 */
	public void setParam_value1(String param_value1)
	{
		this.param_value1 = param_value1;
	}
	/**
	 * @return param_value2
	 */
	public String getParam_value2()
	{
		return param_value2;
	}
	/**
	 * @param param_value2 要设置的 param_value2
	 */
	public void setParam_value2(String param_value2)
	{
		this.param_value2 = param_value2;
	}
	/**
	 * @return param_value3
	 */
	public String getParam_value3()
	{
		return param_value3;
	}
	/**
	 * @param param_value3 要设置的 param_value3
	 */
	public void setParam_value3(String param_value3)
	{
		this.param_value3 = param_value3;
	}
	/**
	 * @return param_value4
	 */
	public String getParam_value4()
	{
		return param_value4;
	}
	/**
	 * @param param_value4 要设置的 param_value4
	 */
	public void setParam_value4(String param_value4)
	{
		this.param_value4 = param_value4;
	}
	/**
	 * @return param_value5
	 */
	public String getParam_value5()
	{
		return param_value5;
	}
	/**
	 * @param param_value5 要设置的 param_value5
	 */
	public void setParam_value5(String param_value5)
	{
		this.param_value5 = param_value5;
	}
	/**
	 * @return param_value6
	 */
	public String getParam_value6()
	{
		return param_value6;
	}
	/**
	 * @param param_value6 要设置的 param_value6
	 */
	public void setParam_value6(String param_value6)
	{
		this.param_value6 = param_value6;
	}
	/**
	 * @return param_value7
	 */
	public String getParam_value7()
	{
		return param_value7;
	}
	/**
	 * @param param_value7 要设置的 param_value7
	 */
	public void setParam_value7(String param_value7)
	{
		this.param_value7 = param_value7;
	}
	/**
	 * @return param_value8
	 */
	public String getParam_value8()
	{
		return param_value8;
	}
	/**
	 * @param param_value8 要设置的 param_value8
	 */
	public void setParam_value8(String param_value8)
	{
		this.param_value8 = param_value8;
	}
	/**
	 * @return param_value9
	 */
	public String getParam_value9()
	{
		return param_value9;
	}
	/**
	 * @param param_value9 要设置的 param_value9
	 */
	public void setParam_value9(String param_value9)
	{
		this.param_value9 = param_value9;
	}
	/**
	 * @return param_value10
	 */
	public String getParam_value10()
	{
		return param_value10;
	}
	/**
	 * @param param_value10 要设置的 param_value10
	 */
	public void setParam_value10(String param_value10)
	{
		this.param_value10 = param_value10;
	}
}